package com.xpgk.mvc.collection.measurement;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.base.BaseValidator;
import com.xpgk.mvc.bentryRegister.Customer;

public class CollectionTemporaryValidator extends BaseValidator {
	
	private static final Log log = Log.getLog(CollectionTemporaryValidator.class);
	
	
	
	
	@Override
	protected void validate(Controller c) {
		String actionKey = getActionKey();
		if(actionKey.equals("/measurement/cesuan"))
		{
			validateDouble("temporary.weight", 30, 400, "msg", "请输入正确的体重，单位斤");
			validateInteger("temporary.height", 50, 300, "msg", "请输入正确的身高，单位cm");
			validateInteger("temporary.age", 1, 100, "msg", "请输入正确的年龄");
			validateMobile("temporary.phone","msg", "请输入正确的手机号");
			validateString("temporary.name", 2, 8, "msg", "请输姓名");
		}
		else if(actionKey.equals("/measurement/saveTemp")){
				valiSaveTemp();
		}
	}
	
	protected void  valiSaveTemp()
	{
		validateDouble("delWeight",-50, 300, "msg", "请先进行咨询测算获取应减斤数");
		validateDouble("customerAgreement.std_weight",30, 400, "msg", "请先进行咨询测算获取标准体重");
		validateDouble("temporary.weight", 30, 400, "msg", "请输入正确的体重，单位斤");
		validateInteger("temporary.height", 50, 300, "msg", "请输入正确的身高，单位cm");
		validateInteger("temporary.age", 1, 100, "msg", "请输入正确的年龄");
//		validateMobile("temporary.phone","msg", "请输入正确的手机号");
		if (StrKit.notBlank(controller.getPara("temporary.phone"))){
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("column", Customer.column_phone);
			String sql = BaseService.getSqlByBeetl(Customer.sqlId_column, param);
			Customer cc=Customer.dao.findFirst(sql,controller.getPara("temporary.phone"));
			if(null != cc)
			{
				addError("msg", "该手机号已被注册!");
			}
		}else{
			addError("msg", "请填写手机号码!");
		}
		validateString("temporary.name", 2, 8, "msg", "请输姓名");
	}
	

	@Override
	protected void handleError(Controller c) {
//		c.keepModel(Temporary.class);
//		BaseController bc = (BaseController)c;
//		bc.setMessage(false);
		String actionkey = getActionKey();
		if(actionkey.equals("/measurement/cesuan")){
			c.renderJson();
		}
		if(actionkey.equals("/measurement/saveTemp")){
			c.renderJson();
		}
	}

}
